﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Elements.Shapes
{
    /// <summary>
    /// 平行四边形定义
    /// </summary>
    public class ParallelogramShapeV5 : ShapeBaseV4
    {
        private GraphicsPath GetGP()
        {
            var x2 = Rect.X;
            var y2 = Rect.Y;
            var w2 = Rect.Width;
            var h2 = Rect.Height;

            var f = w2 / 5;

            //左-上-右-下
            var r0 = new Point(x2 + f, y2);
            var r1 = new Point(x2 + w2, y2);
            var r2 = new Point(x2 + w2 - f, y2 + h2);
            var r3 = new Point(x2, y2 + h2);

            var path = new GraphicsPath();
            path.Reset();
            path.AddPolygon(new Point[] { r0, r1, r2, r3 });
            path.CloseFigure();
            return path;
        }

        public override void Draw(Graphics g)
        {
            var path = GetGP();

            g.FillPath(new SolidBrush(BackgroundColor), path);

            g.DrawString(Text, TextFont, new SolidBrush(FontColor), Rect,
                new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
        }

        public override Point GetLinkPoint(ShapeLinkPoints linkPoint)
        {
            int ptX = 0;
            int ptY = 0;
            switch (linkPoint)
            {
                case ShapeLinkPoints.左:
                    ptX = Rect.X + Rect.Width / 10;
                    ptY = Rect.Y + Rect.Height / 2;
                    break;
                case ShapeLinkPoints.上:
                    ptX = Rect.X + Rect.Width / 2 + Rect.Width / 10;
                    ptY = Rect.Y;
                    break;
                case ShapeLinkPoints.右:
                    ptX = Rect.X + Rect.Width - Rect.Width / 10;
                    ptY = Rect.Y + Rect.Height / 2;
                    break;
                case ShapeLinkPoints.下:
                default:
                    ptX = Rect.X + Rect.Width / 2 - Rect.Width / 10;
                    ptY = Rect.Y + Rect.Height;
                    break;
            }
            return new Point(ptX, ptY);
        }

        public override Rectangle GetLinkRect(ShapeLinkPoints linkPoint)
        {
            int ptX = 0;
            int ptY = 0;
            int w = 10, h = 10, w_half = 5, h_half = 5;
            switch (linkPoint)
            {
                case ShapeLinkPoints.左:
                    ptX = Rect.X + Rect.Width / 10 - w_half;
                    ptY = Rect.Y + Rect.Height / 2 - h_half;
                    break;
                case ShapeLinkPoints.上:
                    ptX = Rect.X + Rect.Width / 2 + Rect.Width / 10 - w_half;
                    ptY = Rect.Y - h_half;
                    break;
                case ShapeLinkPoints.右:
                    ptX = Rect.X + Rect.Width - Rect.Width / 10 - w_half;
                    ptY = Rect.Y + Rect.Height / 2 - h_half;
                    break;
                case ShapeLinkPoints.下:
                default:
                    ptX = Rect.X + Rect.Width / 2 - Rect.Width / 10 - w_half;
                    ptY = Rect.Y + Rect.Height - h_half;
                    break;
            }
            return new Rectangle(ptX, ptY, w, h);
        }
        public override bool IsHitShape(Point mousePt)
        {
            var gp = GetGP();
            return gp.IsVisible(mousePt);
        }

    }
}
